package com.zijiebutiao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zijiebutiao.pojos.TOrdersetting;
import com.zijiebutiao.service.TOrdersettingService;
import com.zijiebutiao.mapper.TOrdersettingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author ZhangQingTian
* @description 针对表【t_ordersetting】的数据库操作Service实现
* @createDate 2023-02-28 20:29:24
*/
@Transactional(rollbackFor = Exception.class)
@Service
public class TOrdersettingServiceImpl extends ServiceImpl<TOrdersettingMapper, TOrdersetting>
    implements TOrdersettingService{
    @Autowired
    private TOrdersettingMapper tOrdersettingMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addOrUpdate(List<TOrdersetting> list) {
        list.stream().forEach(s->{
            LambdaQueryWrapper<TOrdersetting> queryWrapper= Wrappers.lambdaQuery();
            queryWrapper.eq(TOrdersetting::getOrderdate,s.getOrderdate());
            TOrdersetting ordersetting=this.getOne(queryWrapper);
            if (ordersetting==null){
                tOrdersettingMapper.insert(s);
            }else {
                if (s.getNumber()>=ordersetting.getReservations()){
                    tOrdersettingMapper.update(s,queryWrapper);
                }
            }
        });

        return true;
    }

    @Override
    public List<TOrdersetting> findByMonth(String beginDay, String endDay) {
        LambdaQueryWrapper<TOrdersetting> queryWrapper=Wrappers.lambdaQuery();
        queryWrapper.between(TOrdersetting::getOrderdate,beginDay,endDay);

        return tOrdersettingMapper.selectList(queryWrapper);
    }
}




